<?php


namespace app\admin\controller\invoice;


use app\admin\model\business\BusinessOrder as BusinessOrderModel;
use app\common\controller\Adminbase;
use think\Db;

class Invoice extends Adminbase
{
    protected $searchFields = 'order_no';

    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        $status = input('param.status/d');
        if ($this->request->isAjax()) {
            $where = array();
            if (isset($status)) {
                $where[] = ['status', '=', $status];
            }
            $page = input('page/d', 1);
            $limit = input('limit/d', 10);
            $_list = Db::name('invoice')
                ->where($where)
                ->page($page, $limit)
                ->order('create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }
            $total = Db::name('invoice')
                ->where($where)
                ->page($page, $limit)
                ->order('create_time desc')->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        if (isset($status)) {
            $this->assign('status', $status);
        }
        return $this->fetch('invoice/index');
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $insert_data = array(
                'id' => $data['id'],
                'status' => 1,
            );
            $stmt = Db::name('invoice')->update($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        } else {
            $id = input('param.id/d');
            $data_info = Db::name('invoice')->find($id);
            $this->assign('data', $data_info);
            return $this->fetch('invoice/edit');
        }
    }

    //开发票
    public function ca()
    {
        $ids = $this->request->param();
        if (Db::name('invoice')->where('id', 'in', $ids['id'])->update(['status' => 1])) {
            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }
}